#!/bin/bash

# block read testing
# give it a device and it will go after it


if [ -z "$3" ] ; then
    echo "Usage: $0 device count percent"
    echo "	device : device to test(eg. /dev/sda)"
    echo "	count : number of blocks to test for each chunk"
    echo "	percent : percentage of disk surface coverage"
    exit 2
fi

renice +5 -p $$
bsize=1024
count=$2
percent=$3

disksize=`fdisk -l $1 | grep -m 1 $1 | awk '{print $5}'`
let disksize="$[disksize/bsize]";
let blocks=$(echo "($disksize * $percent / 100)/$count" | bc)
let normmult=$(echo "($disksize - $count) / 32767" | bc)
echo "
Testing disk, random swap
Disk coverage = $percent%, $((count*blocks*bsize/1024))KB 
Chunk Size = $count x $((bsize/1024))KB";
for ((i=0; i <= blocks ; i+=1))
do
  let "start=($RANDOM*normmult)"
  let "end=start+count"
  echo "Testing at location $(((start+end)*50/disksize))% - $i of $blocks  "
  badblocks -b $bsize $1 $end $start 2&>1
done
